<template>
  <div class="container">
    <div class="app-container">
      <el-card class="box-card">
        <template>
          <div class="container">
            <div class="app-container">
              <!-- 角色管理内容 -->
              <div class="role-operate">
                <el-button size="mini" type="primary" @click="showDialog=true">添加角色</el-button>
              </div>
              <!-- 放置table组件 -->
              <el-table :data="list">
                <!-- 放置列 -->
                <el-table-column prop="name" align="center" width="200" label="角色" />
                <el-table-column prop="state" align="center" width="200" label="启用">
                  <template v-slot="{ row }">
                    <span>  {{ row.state === 1 ? "已启用" : row.state === 0 ? "未启用" : "无" }} </span>
                  </template>
                </el-table-column>
                <el-table-column prop="description" align="center" label="描述" />
                <el-table-column align="center" label="操作">
                  <template>
                    <el-button size="mini" type="text">分配权限</el-button>
                    <el-button size="mini" type="text">编辑</el-button>
                    <el-button size="mini" type="text">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <!-- 放置分页组件 -->
              <div class="block">

                <el-pagination
                  :page-sizes="[5,6,7]"
                  :page-size="params.pagesize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total"
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                />
              </div>
            </div>
          </div>
        </template>
      </el-card>
      <!-- 表单弹框 -->
      <el-dialog width="500px" title="新增角色" :visible.sync="showDialog" @close="close">
        <!-- 表单内容 -->
        <el-form ref="addForm" label-width="120px" :model="formData" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="formData.name" style="width:300px" size="mini" />
          </el-form-item>
          <el-form-item prop="state" label="启用">
            <el-switch v-model="formData.state" size="mini" />
          </el-form-item>
          <el-form-item prop="description" label="角色描述">
            <el-input v-model="formData.description" type="textarea" :rows="3" style="width:300px" size="mini" />
          </el-form-item>
          <el-form-item>
            <el-row type="flex" justify="center">
              <el-col :span="12">
                <el-button type="primary" size="mini" @click="submitAdd">确定</el-button>
                <el-button size="mini" @click="close">取消</el-button>
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoleListAPI, addRoleAPI } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      list: [],
      params: {
        page: 1,
        pagesize: 5
      },
      total: 0,
      showDialog: false,
      formData: {
        name: '',
        description: '',
        state: 0
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const res = await getRoleListAPI(this.params)
      this.list = res.rows
      console.log(this.list)
      this.total = res.total
    },
    // 分页
    handleSizeChange(val) {
      this.params.pagesize = val
      this.getRoleList()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.getRoleList()
    },
    // 关闭弹窗
    close() {
      this.showDialog = false
      this.formData = {}
      this.$refs.addForm.resetFields()
    },
    // 点击确定新增角色
    submitAdd() {
      this.$refs.addForm.validate(async(isOk) => {
        if (isOk) {
          await addRoleAPI(this.formData)
          this.$message.success('添加角色成功')
          this.close()
          this.getRoleList()
        }
      })
    }
  }
}
</script>
<style scoped>
.role-operate {
  padding: 10px;
}
</style>
